<?php
/************************
 *  Codenova.pl Project
 *  Created by: Kamil Mrzygłód
 *
 *  (c) 2013 by Codenova.pl
 *  All Right Reserved
 ***********************/

namespace Modules\JasminumGallery\Controllers;

use Gotcha\App\Extensions\GotchaRoutingExtension;
use Gotcha\App\Framework\Controller;
use GotchaDatabase\GotchaSettingsQuery;
use GotchaDatabase\JasminumGalleriesCategoriesQuery;
use GotchaDatabase\JasminumGalleriesPeer;
use GotchaDatabase\JasminumGalleriesPhotosQuery;
use GotchaDatabase\JasminumGalleriesQuery;

class DefaultController extends Controller {
    public function getGalleries() {
        // Get thumbnails dir
        $settings = GotchaSettingsQuery::create()
                        ->filterByStrongName('GalleryThumbnailsDir')
                            ->findOne();

        $galleries = JasminumGalleriesQuery::create()
                    ->orderById()
                        ->find();

        // HTML buffer
        $html = '';

        foreach ($galleries as $gallery) {
            $slug = parent::nameToSlug($gallery->getName());

            $html .= '<div id="gallery-'.$gallery->getId().'">';
            $html .= '<header><h2><a title="Zobacz galerię" href="'.GOTCHA_BASE_PATH.'/galeria/'.$slug.'">'.$gallery->getName().'</a></h2></header>';
            $html .= '<a title="Zobacz galerię" href="'.GOTCHA_BASE_PATH.'/galeria/'.$slug.'"><img width="320" src="'.GOTCHA_BASE_PATH.$settings->getValue().$gallery->GetImageSrc().'"></a>';
            $html .= '</div>';
        }

        // Flush buffer
        echo $html;
    }

    public function getGalleryData() {
        // We are checking current URL to get the name
        // of the chosen gallery
        $url = new GotchaRoutingExtension();
        $parts_url = explode('/', $url);

        // We have to convert slug into regular string
        $name = parent::slugToName($parts_url[count($parts_url)-1]);

        // Choose current gallery from database
        $gallery_criteria = new \Criteria();
        $gallery_criteria->add(JasminumGalleriesPeer::NAME, '%'.$name.'%', \Criteria::LIKE);
        $gallery = JasminumGalleriesPeer::doSelectOne($gallery_criteria);

        return $gallery;
    }

    public function getSubgalleries($id) {
        // We choose subgalleries connected with current id
        $subgalleries = JasminumGalleriesCategoriesQuery::create()
                            ->filterByJasminumGalleriesId($id)
                                ->orderById('desc')
                                    ->find();

        if ($subgalleries->count() === 0) {
            return '<p class="info">Nie ma dodanych kategorii.</p>';
        }

        // We some some subgalleries so we can iterate through
        // whole collection

        // Retrieve default gallery dir
        $settings = GotchaSettingsQuery::create()
                        ->filterByStrongName('GalleryMainDir')
                            ->findOne();

        // HTML buffer
        $html = '';

        foreach ($subgalleries as $subgallery) {
            // Add header
            $html .= '<div id="category-'.$subgallery->getName().'" class="subgallery"><header>'.$subgallery->getName().'</header>';
            $html .= '<div id="subgallery-description"><p>'.$subgallery->getDescription().'</p></div>';

            // We rendered header and description - now render some photos.
            $photos = JasminumGalleriesPhotosQuery::create()
                        ->filterByJasminumGalleriesCategoriesId($subgallery->getId())
                            ->orderById('desc')
                                ->find();

            if ($photos->count() === 0) {
                $html .= '<p class="info">Nie ma dodanych zdjęć.</p>';
            } else {
                // We found some photos
                $html .= '<div class="gallery-photos clearfix">';

                foreach ($photos as $photo) {
                    $html .= '<div class="photo"><a href="'.GOTCHA_BASE_PATH.$settings->getValue().$photo->getPhotoSrc().'" rel="lightbox['.parent::nameToSlug($subgallery->getName()).']" title="Zdjęcia - '.$subgallery->getName().'"><img src="'.GOTCHA_BASE_PATH.'/JasminumGallery/RenderPhoto?src='.$photo->getPhotoSrc().'&width=220"></a></div>';
                }

                $html .= '</div>';
            }

            $html .= '</div>';
        }

        return $html;
    }

    public function getGallery() {
        return parent::View();
    }
}